/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.library;

import java.util.Vector;

/**
 * Class, representing an ordered list of <code>Version</code> type objects.
 */
public class VersionVector extends Vector<Version> {
	private static final long serialVersionUID = -7174612567374624832L;

	@Override
	public synchronized boolean equals(Object o) {
		if (o == null)
			return false;
		if (o instanceof VersionVector)
			return equals((VersionVector) o);
		return false;
	}

	@Override
	public synchronized int hashCode() {
		int value = 0;

		for (Version v : this)
			value += v.hashCode();

		return value;
	}

	/**
	 * Check if this vector equals a given one. Two vectors are equal, when containing the same versions, regardless of
	 * order
	 * 
	 * @param vector A given VersionVector
	 * @return true if the vectors are equal, false otherwise
	 */
	public boolean equals(VersionVector vector) {
		if (vector.size() != this.size())
			return false;

		for (Version localVersion : this) {
			boolean found = false;

			for (Version remoteVersion : vector)
				if (localVersion.equals(remoteVersion)) {
					found = true;
					break;
				}
			if (!found)
				return false;
		}

		return true;
	}
}